<%--
Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
--%>

<%@ Control Language="C#" ClassName="GrurrahCreateControl" Inherits="Grr.Web.GrurrahControl" %>
<%@ Register Src="WYSIWYGControl.ascx" TagName="WYSIWYGControl" TagPrefix="uc1" %>
<%@ Register Assembly="Gaia.WebWidgets" Namespace="Gaia.WebWidgets" TagPrefix="gaia" %>

<%@ Import Namespace="Grr.Web" %>
<%@ Import Namespace="Grr.Model" %>

<script runat="server">

    private static readonly string DefaultTitle = "Click here to enter title";
    
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        string title = Request.QueryString["g"];
        if (title != null)
        {
            string t = GrurrahString.ToUserString (title);
            Page.Title = t;
            TitleInPlaceEdit.Text = t;
        }
    }

    protected override bool OnBubbleEvent(object source, EventArgs args)
    {
        WYSIWYGCommandEventArgs we = args as WYSIWYGCommandEventArgs;

        if (we != null)
        {
            if (we.Command == WYSIWYGCommand.Cancel)
                Cancel();
            else
                Save();
        }
        return base.OnBubbleEvent(source, args);
    }

    protected void Cancel()
    {
        RaiseBubbleEvent(this, new GrurrahPageModeChangedEventArgs(GrurrahPageMode.Vacant));
    }
    
    protected void Save ()
    {
        string name = TitleInPlaceEdit.Text;

        if (name == DefaultTitle)
        {
            RaiseBubbleEvent(this, new UserNotificationEventArgs("You must enter a title for your Grurrah."));
            return;
        }
        
        string text = WYSIWYGControl.Text;
        GrurrahUser user = Session["CurrentUser"] as GrurrahUser;

        if (user == null)
        {
            RaiseBubbleEvent(this, new UserNotificationEventArgs("Sorry, you must be logged in to create Grurrahs."));
            return;
        }
        
        Grurrah g = Grurrah.Create (GrurrahString.ToDbString (name), text, user.Name, Request.UserHostAddress);
        Session["CurrentGrurrah"] = g;
        
        RaiseBubbleEvent(this, new GrurrahPageModeChangedEventArgs(GrurrahPageMode.Read));
    }

    protected void TitleInPlaceEdit_TextChanged(object sender, EventArgs e)
    {
        Grurrah g = Grurrah.QueryForByName(TitleInPlaceEdit.Text);

        if (g != null)
        {
            Session["CurrentGrurrah"] = g;
            RaiseBubbleEvent(this, new UserNotificationEventArgs("This Grurrah already exists, you may now edit it's text."));
            RaiseBubbleEvent(this, new GrurrahPageModeChangedEventArgs(GrurrahPageMode.Edit));
        }
    }
</script>

<table style="margin:0px 15px 15px 15px;">
        <tr>
            <td>
                <h1>
                    <gaia:InPlaceEdit ID="TitleInPlaceEdit" SingleLine="True" CssClass="create_grurrah_title" Text="Click here to enter title" runat="server" OnTextChanged="TitleInPlaceEdit_TextChanged"></gaia:InPlaceEdit>
                </h1>
            </td>
        </tr>
        <tr>
            <td style="vertical-align:top;" valign="top">
                <uc1:WYSIWYGControl ID="WYSIWYGControl" runat="server" />
            </td>
        </tr>
    </table>
